package com.xyz.tasks.tdo;

import com.free.exception.SystemException;
import com.free.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.IntStream;

public class TaskTimeObject {

    private Integer second = 0;

    private Integer minute = 0;

    private Integer hour = 0;

    private String timeInfo;

    private Date todayDateInfo;

    private Date nextDateInfo;

    public TaskTimeObject(String timeInfo) throws SystemException {
        if(StringUtils.isEmpty(timeInfo)){
            return;
        }
        this.timeInfo = timeInfo;
        String[] nums = timeInfo.split(":");
        numberTransfer(nums);
        todayDateInfo = todayInfo();
        nextDateInfo = nextDayInfo(todayDateInfo);
    }

    public Date getTodayDateInfo() {
        return todayDateInfo;
    }

    public void setTodayDateInfo(Date todayDateInfo) {
        this.todayDateInfo = todayDateInfo;
    }

    public Date getNextDateInfo() {
        return nextDateInfo;
    }

    public void setNextDateInfo(Date nextDateInfo) {
        this.nextDateInfo = nextDateInfo;
    }

    public Integer getSecond() {
        return second;
    }

    public void setSecond(Integer second) {
        this.second = second;
    }

    public Integer getMinute() {
        return minute;
    }

    public void setMinute(Integer minute) {
        this.minute = minute;
    }

    public Integer getHour() {
        return hour;
    }

    public void setHour(Integer hour) {
        this.hour = hour;
    }

    private void numberTransfer(String[] nums){
        if(nums == null || nums.length == 0){
            return;
        }
        List<String> numList = Arrays.asList(nums);
        IntStream.range(0,2).forEach(x->{
           if( x == 0) {
               hour = Integer.parseInt(numList.get(x));
           } else if( x == 1) {
               minute = Integer.parseInt(numList.get(x));
           } else if(x == 2){
               second = Integer.parseInt(numList.get(x));
           }
        });
    }

    private Date todayInfo() throws SystemException {
        String yyyyMMdd = DateUtils.now(DateUtils.FormatEnum.yyyy_MM_dd);
        String dateTime = yyyyMMdd + " " + numDouble(hour) + ":" + numDouble(minute) + ":" + numDouble(second);
        return DateUtils.convert2Date(dateTime, DateUtils.FormatEnum.yyyy_MM_dd_HH_mm_ss);
    }

    private Date nextDayInfo(Date todayInfo){
        return DateUtils.nextDate(todayInfo,1);
    }

    private String numDouble(Integer num){
        String res = "";
        if(num < 10){
            res = "0";
        }
        return res + num;
    }

}
